package reading.util.utils;

/**
 * @author TXW
 * @version V1.0
 * @Title: ${file_name}
 * @date 2018/11/16 10:34
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import reading.util.aes.DesUtil;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;
import java.io.IOException;
import java.util.Properties;

/**
 * 文件名称：邮件发送工具类
 * 内容摘要：
 * 创建人： tanxinwang
 * 创建日期： 2017年1月17日
 * 版本号： v1.0.0
 * 版权所有： (C)2001-2015
 * 修改记录1
 * 修改日期：
 * 版本号：
 * 修改人：
 * 修改内容：
 *
 * @author txw
 **/
public class SendEmailUtil {
    private final static Logger logger = LoggerFactory.getLogger(SendEmailUtil.class);
    private final static String HOST = "smtp.163.com";
    private final static String USERNAME = "18983260339@163.com";
    private final static String PASSWORD = "498EEDCD79C13267B2348C82598929AB";
    private final static String CONNECT_PWD = "C26FA01566B51EC9";
    private static String PWD;

    static {
        try {
            PWD = DesUtil.decrypt(PASSWORD);
        } catch (Exception e) {
            logger.error("解密失败", e);
        }
    }

    /**
     * 发送消息
     *
     * @param title
     * @param email
     * @param msg
     * @return void
     * @throws Exception
     * @throws IOException
     */
    public static void send(String title, String email, String msg) throws IOException, Exception {
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.auth", "true");
        MyAuthenticator myAuth = new MyAuthenticator(USERNAME, "123qwe123");
        Session session = Session.getDefaultInstance(props, myAuth);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(USERNAME));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        Multipart multipart = new MimeMultipart();
        message.setSubject(title);
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(msg);
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect(HOST, USERNAME, "love520");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    /**
     * 发送消息及附件
     *
     * @param title
     * @param email
     * @param msg
     * @param fileName
     * @param filePath
     * @return void
     * @throws Exception
     * @throws IOException
     */
    public static void send(String title, String email, String msg, String fileName, String filePath) throws IOException, Exception {
        Properties props = new Properties();
        props.put("mail.smtp.host", HOST);
        props.put("mail.smtp.auth", "true");
        MyAuthenticator myAuth = new MyAuthenticator(USERNAME, "123qwe123");
        Session session = Session.getDefaultInstance(props, myAuth);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(USERNAME));
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
        Multipart multipart = new MimeMultipart();
        message.setSubject(title);
        BodyPart messageBodyPart = new MimeBodyPart();
        messageBodyPart.setText(msg);
        multipart.addBodyPart(messageBodyPart);
        messageBodyPart = new MimeBodyPart();
        DataSource source = new FileDataSource(filePath);
        messageBodyPart.setDataHandler(new DataHandler(source));
        messageBodyPart.setFileName(MimeUtility.encodeText(fileName, "UTF-8", null));
        multipart.addBodyPart(messageBodyPart);
        message.setContent(multipart);
        message.saveChanges();
        Transport transport = session.getTransport("smtp");
        transport.connect(HOST, USERNAME, "love520");
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    private static class MyAuthenticator extends Authenticator {
        private String strUser;
        private String strPwd;

        public MyAuthenticator(String user, String password) {
            this.strUser = user;
            this.strPwd = password;
        }

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(strUser, strPwd);
        }
    }
}